<?php
/* 
 * ebiz front
 * 
 * ImageController
 * 
 * @author allen
 * 
 */
require_once '../app/core/controllers/BaseController.php';

class ImageController extends BaseController 
{
	
    public function indexaction ($n=0)
    {
        //header("Content-type:image/PNG");
        $w = 150;
        $h = 60; 
//        $fontface =  "D:/wamp/www/front/pub/arial.ttf";
        $fontface =  "../pub/arial.ttf";
        $str = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
        $code = "";
        for ($i = 0; $i < 6; $i ++) {
            $Xi = mt_rand(0, strlen($str) / 2);
            if ($Xi % 2)
                $Xi += 1;
            $code .= substr($str, $Xi, 1);
        }
        $im = imagecreatetruecolor($w, $h);
//        $im = imagecreatefrompng("yzpic.png");
        $bkcolor = imagecolorallocate($im, 250, 250, 250);
        imagefill($im, 0, 0, $bkcolor);
//         for($i = 0; $i<15; $i++){
//            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
//            imagearc($im, mt_rand(-10, $w), mt_rand(-10, $h), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
//        }
//        for($i = 0; $i<255; $i++){
//            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
//            imagesetpixel($im, mt_rand(0, $w), mt_rand(0, $h), $fontcolor);
//        }
        
        for ($i = 0; $i < 6; $i ++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
            $codex = iconv("GB2312", "UTF-8", substr($code, $i * 2, 2));
           
            imagettftext($im, 18, mt_rand(- 20, 20), 40 * $i + 20, 35, $fontcolor, $fontface, $codex);
        }
        $a = rand(100000, 999999);
        $authpngpath = dirname(dirname(dirname(__FILE__)));
         
//        if (! is_dir($authpngpath . DIRECTORY_SEPARATOR . 'pub'  . DIRECTORY_SEPARATOR . 'activecode' . DIRECTORY_SEPARATOR)) {
//            mkdir($authpngpath . DIRECTORY_SEPARATOR . 'pub' . DIRECTORY_SEPARATOR . 'activecode' . DIRECTORY_SEPARATOR);
//             
//        }
//        if (is_dir($authpngpath . DIRECTORY_SEPARATOR . 'pub'  . DIRECTORY_SEPARATOR . 'activecode' . DIRECTORY_SEPARATOR)) {
//            $this->full_rmdir($authpngpath . DIRECTORY_SEPARATOR . 'pub'  . DIRECTORY_SEPARATOR . 'activecode' . DIRECTORY_SEPARATOR);
//        }
        imagepng($im, $authpngpath . DIRECTORY_SEPARATOR . 'pub' . DIRECTORY_SEPARATOR . 'activecode' . DIRECTORY_SEPARATOR . $a . '.png');
//        echo '<img id="codeimage" src="/activecode/' . $a . '.png" /><input type="hidden" name="codedata" id="codedata" value="' . strtolower($code) . '">';
        if($n==1){
            $result = '<img width="100" heigh="50" id="codeimage1" src="/activecode/' . $a . '.png" /><input type="hidden" name="codedata" id="codedata1" value="' . strtolower($code) . '"><br/>';	
        }else {
            $result = '<img width="100" heigh="50" id="codeimage" src="/activecode/' . $a . '.png" /><input type="hidden" name="codedata" id="codedata" value="' . strtolower($code) . '"><br/>';
        }
        return $result;
    }
    

}


